import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    name: 'auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/register/index'),
    hidden: true
  },
  {
    path: '/home',
    component: () => import('@/views/home/index'),
    hidden: true,
    children: [
      {
        path: '/main-one',
        name: 'mainOne',
        component: () => import('@/views/home/components/MainOne'),
        meta: { title: '主页', icon: 'el-icon-s-home', noCache: true },
        hidden: true
      },
      {
        path: '/viewNotice/:id',
        name: 'viewNotice',
        component: () => import('@/views/home/components/NoticeDetail'),
        hidden: true
      },
      {
        path: '/basicData/:type',
        name: 'basicData',
        component: () => import('@/views/home/components/BasicData'),
        hidden: true
      },
      {
        path: '/noticeInfo/:type',
        name: 'noticeInfo',
        component: () => import('@/views/home/components/NoticeInfo'),
        hidden: true
      },
      {
        path: '/organize-user',
        name: 'organizeUser',
        component: () => import('@/views/home/components/Person'),
        hidden: true
      },
      {
        path: '/comment',
        name: 'comment',
        component: () => import('@/views/home/components/Comment'),
        hidden: true
      },
    ]
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/main-one',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'el-icon-s-home', affix: true }
      }
    ]
  },

  {
    path: '/announcement',
    component: Layout,
    redirect: '/announcement/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/announcement/index'),
        name: 'Profile',
        meta: { title: '公告中心', icon: 'el-icon-video-camera-solid', noCache: true }
      }
    ]
  },
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
// export const asyncRoutes = [
//   {
//     path: '/permission',
//     component: Layout,
//     redirect: '/permission/page',
//     alwaysShow: true, // will always show the root menu
//     name: 'Permission',
//     meta: {
//       title: 'Permission',
//       icon: 'lock',
//       roles: ['admin', 'editor'] // you can set roles in root nav
//     },
//     children: [
//       {
//         path: 'page',
//         component: () => import('@/views/permission/page'),
//         name: 'PagePermission',
//         meta: {
//           title: 'Page Permission',
//           roles: ['admin'] // or you can only set roles in sub nav
//         }
//       },
//       {
//         path: 'directive',
//         component: () => import('@/views/permission/directive'),
//         name: 'DirectivePermission',
//         meta: {
//           title: 'Directive Permission'
//           // if do not set roles, means: this page does not require permission
//         }
//       },
//       {
//         path: 'role',
//         component: () => import('@/views/permission/role'),
//         name: 'RolePermission',
//         meta: {
//           title: 'Role Permission',
//           roles: ['admin']
//         }
//       }
//     ]
//   },

//   {
//     path: '/icon',
//     component: Layout,
//     children: [
//       {
//         path: 'index',
//         component: () => import('@/views/icons/index'),
//         name: 'Icons',
//         meta: { title: 'Icons', icon: 'icon', noCache: true }
//       }
//     ]
//   },

//   /** when your routing map is too long, you can split it into small modules **/
//   componentsRouter,
//   chartsRouter,
//   nestedRouter,
//   tableRouter,

//   {
//     path: '/example',
//     component: Layout,
//     redirect: '/example/list',
//     name: 'Example',
//     meta: {
//       title: 'Example',
//       icon: 'el-icon-s-help'
//     },
//     children: [
//       {
//         path: 'create',
//         component: () => import('@/views/example/create'),
//         name: 'CreateArticle',
//         meta: { title: 'Create Article', icon: 'edit' }
//       },
//       {
//         path: 'edit/:id(\\d+)',
//         component: () => import('@/views/example/edit'),
//         name: 'EditArticle',
//         meta: { title: 'Edit Article', noCache: true, activeMenu: '/example/list' },
//         hidden: true
//       },
//       {
//         path: 'list',
//         component: () => import('@/views/example/list'),
//         name: 'ArticleList',
//         meta: { title: 'Article List', icon: 'list' }
//       }
//     ]
//   },

//   {
//     path: '/tab',
//     component: Layout,
//     children: [
//       {
//         path: 'index',
//         component: () => import('@/views/tab/index'),
//         name: 'Tab',
//         meta: { title: 'Tab', icon: 'tab' }
//       }
//     ]
//   },

//   {
//     path: '/error',
//     component: Layout,
//     redirect: 'noRedirect',
//     name: 'ErrorPages',
//     meta: {
//       title: 'Error Pages',
//       icon: '404'
//     },
//     children: [
//       {
//         path: '401',
//         component: () => import('@/views/error-page/401'),
//         name: 'Page401',
//         meta: { title: '401', noCache: true }
//       },
//       {
//         path: '404',
//         component: () => import('@/views/error-page/404'),
//         name: 'Page404',
//         meta: { title: '404', noCache: true }
//       }
//     ]
//   },

//   {
//     path: '/error-log',
//     component: Layout,
//     children: [
//       {
//         path: 'log',
//         component: () => import('@/views/error-log/index'),
//         name: 'ErrorLog',
//         meta: { title: 'Error Log', icon: 'bug' }
//       }
//     ]
//   },

//   {
//     path: '/excel',
//     component: Layout,
//     redirect: '/excel/export-excel',
//     name: 'Excel',
//     meta: {
//       title: 'Excel',
//       icon: 'excel'
//     },
//     children: [
//       {
//         path: 'export-excel',
//         component: () => import('@/views/excel/export-excel'),
//         name: 'ExportExcel',
//         meta: { title: 'Export Excel' }
//       },
//       {
//         path: 'export-selected-excel',
//         component: () => import('@/views/excel/select-excel'),
//         name: 'SelectExcel',
//         meta: { title: 'Export Selected' }
//       },
//       {
//         path: 'export-merge-header',
//         component: () => import('@/views/excel/merge-header'),
//         name: 'MergeHeader',
//         meta: { title: 'Merge Header' }
//       },
//       {
//         path: 'upload-excel',
//         component: () => import('@/views/excel/upload-excel'),
//         name: 'UploadExcel',
//         meta: { title: 'Upload Excel' }
//       }
//     ]
//   },

//   {
//     path: '/zip',
//     component: Layout,
//     redirect: '/zip/download',
//     alwaysShow: true,
//     name: 'Zip',
//     meta: { title: 'Zip', icon: 'zip' },
//     children: [
//       {
//         path: 'download',
//         component: () => import('@/views/zip/index'),
//         name: 'ExportZip',
//         meta: { title: 'Export Zip' }
//       }
//     ]
//   },

//   {
//     path: '/pdf',
//     component: Layout,
//     redirect: '/pdf/index',
//     children: [
//       {
//         path: 'index',
//         component: () => import('@/views/pdf/index'),
//         name: 'PDF',
//         meta: { title: 'PDF', icon: 'pdf' }
//       }
//     ]
//   },
//   {
//     path: '/pdf/download',
//     component: () => import('@/views/pdf/download'),
//     hidden: true
//   },

//   {
//     path: '/theme',
//     component: Layout,
//     children: [
//       {
//         path: 'index',
//         component: () => import('@/views/theme/index'),
//         name: 'Theme',
//         meta: { title: 'Theme', icon: 'theme' }
//       }
//     ]
//   },

//   {
//     path: '/clipboard',
//     component: Layout,
//     children: [
//       {
//         path: 'index',
//         component: () => import('@/views/clipboard/index'),
//         name: 'ClipboardDemo',
//         meta: { title: 'Clipboard', icon: 'clipboard' }
//       }
//     ]
//   },

//   {
//     path: 'external-link',
//     component: Layout,
//     children: [
//       {
//         path: 'https://github.com/PanJiaChen/vue-element-admin',
//         meta: { title: 'External Link', icon: 'link' }
//       }
//     ]
//   },

//   // 404 page must be placed at the end !!!
//   { path: '*', redirect: '/404', hidden: true }
// ]

// 导航路由
// 角色身份数组
let roles = new Map
roles.set("superManager", '772a7b13dabc015c495d9056aed54a85')
roles.set("manager", 'f2779e805950be76390fff2b186879ad')
roles.set("purchaser", 'ebeda8021fccea8209458c3f89b80aad')
roles.set("supplier", 'ebd95568c552bc9e808c9b6b680eb6f8')

export const asyncRoutes = [
  {
    path: '/account-manage', // 超级管理员
    component: Layout,
    redirect: '/account-manage/index',
    meta: { roles: [roles.get('superManager')] },
    children: [
      {
        path: 'index',
        component: () => import('@/views/account-manage/index'),
        name: 'AccountManage',
        meta: {
          title: '管理员信息',
          icon: 'el-icon-user',
          roles: [roles.get('superManager')]
        }
      }
    ]
  },
  {
    path: '/roles-manage',
    component: Layout,
    redirect: '/roles-manage/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/roles-manage/index'),
        name: 'RolesManage',
        meta: {
          title: '角色管理',
          icon: 'el-icon-s-custom',
          roles: [roles.get('superManager')] // or you can only set roles in sub nav
        }
      }
    ]
  },
  {
    path: '/organizations-manage',
    component: Layout,
    redirect: '/organizations-manage/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/organizations-manage/index'),
        name: 'OrganizationsManage',
        meta: {
          title: '组织管理',
          icon: 'el-icon-office-building',
          roles: [roles.get('superManager')] // or you can only set roles in sub nav
        }
      }
    ]
  },

  // 管理员路由
  // 人员管理
  {
    path: '/users-manage',
    component: Layout,
    alwaysShow: true,
    name: 'UsersManage',
    meta: {
      title: '用户管理',
      icon: 'el-icon-user',
      roles: [roles.get('manager')]
    },
    children: [
      {
        path: 'purchaser/index',
        component: () => import('@/views/users-manage/purchaser/index'),
        name: 'Purchaser',
        meta: { title: '采购员', icon: 'el-icon-s-custom', noCache: true }
      },
      {
        path: "/supplier",
        name: 'Supplier',
        redirect: '/supplier/unreviewed/index',
        component: () => import('@/views/users-manage/supplier/index'),
        meta: {
          title: '供应商',
          icon: 'el-icon-user',
          noCache: true
        },
        children: [
          {
            path: '/unreviewed/index',
            component: () => import('@/views/users-manage/supplier/unreviewed/index'),
            name: 'Unreviewed',
            meta: { title: '未审核', icon: 'el-icon-s-custom', noCache: true }
          },
          {
            path: '/audited/index',
            component: () => import('@/views/users-manage/supplier/audited/index'),
            name: 'Audited',
            meta: { title: '已审核', icon: 'el-icon-user-solid', noCache: true }
          }
        ]
      },
    ]
  },
  // 标案管理
  {
    path: '/tender-manage',
    component: Layout,
    alwaysShow: true,
    name: 'TenderManage',
    meta: {
      title: '标案管理',
      icon: 'el-icon-s-order',
      roles: [roles.get('manager')]
    },
    children: [
      {
        path: 'tender-verify',
        component: () => import('@/views/tender-manage/tender-verify/index'),
        name: 'TenderVerify',
        meta: { title: '标案审核', icon: 'el-icon-s-claim', noCache: true }
      },
      {
        path: 'tender-manage',
        component: () => import('@/views/tender-manage/tender-manage/index'),
        name: 'TenderManagement',
        meta: { title: '标案记录', icon: 'el-icon-s-management', noCache: true }
      }
    ]
  },
  // 主页管理 管理员、超级管理员、采购员都可以访问
  {
    path: '/home-manage',
    component: Layout,
    alwaysShow: true,
    name: 'HomeManage',
    meta: {
      title: '主页管理',
      icon: 'el-icon-setting',
      roles: [roles.get('manager'), roles.get('superManager'),roles.get('purchaser')]
    },
    children: [
      {
        path: 'announcement/index',
        component: () => import('@/views/home-manage/announcement/index'),
        name: 'Announcement',
        meta: { title: '公告管理', icon: 'el-icon-bell', noCache: true }
      },
      {
        path: 'image-carousel/index',
        component: () => import('@/views/home-manage/image-carousel/index'),
        name: 'ProductTypeManage',
        meta: { title: '轮播图管理', icon: 'el-icon-picture-outline', noCache: true }
      },
    ]
  },
  // 基础资料管理 管理员、超级管理员都可以访问
  {
    path: '/basic-data',
    component: Layout,
    alwaysShow: true,
    name: 'BasicData',
    meta: {
      title: '基础资料',
      icon: 'el-icon-s-data',
      roles: [roles.get('manager'), roles.get('superManager')]
    },
    children: [
      {
        path: 'product-manage/index',
        component: () => import('@/views/basic-data/product-manage/index'),
        name: 'ProductManage',
        meta: { title: '产品管理', icon: 'el-icon-goods', noCache: true }
      },
      {
        path: 'product-type-manage/index',
        component: () => import('@/views/basic-data/product-type-manage/index'),
        name: 'ProductTypeManage',
        meta: { title: '类型管理', icon: 'el-icon-star-off', noCache: true }
      },
      {
        path: 'project-manage/index',
        component: () => import('@/views/basic-data/project-manage/index'),
        name: 'ProjectManage',
        meta: { title: '项目管理', icon: 'el-icon-folder', noCache: true }
      },
      {
        path: 'non-staff/index',
        component: () => import('@/views/basic-data/non-staff/index'),
        name: 'ProjectManage',
        meta: { title: '编外人员', icon: 'el-icon-user-solid', noCache: true }
      },
    ]
  },



  // 采购员路由
  // 审核管理
  {
    path: '/verify-manage',
    component: Layout,
    alwaysShow: true,
    name: 'VerifyManage',
    meta: {
      title: '审核管理',
      icon: 'el-icon-s-check',
      roles: [roles.get('purchaser')]
    },
    children: [
      {
        path: 'bid',
        component: () => import('@/views/verify-manage/bid/index'),
        name: 'Bid',
        meta: { title: '投标审核', icon: 'el-icon-document-checked', noCache: true }
      },
      {
        path: 'tender-records',
        component: () => import('@/views/verify-manage/tender-records/index'),
        name: 'TenderRecords',
        meta: { title: '投标审核记录', icon: 'el-icon-notebook-2', noCache: true }
      },
      {
        path: 'tender-invite-records',
        component: () => import('@/views/verify-manage/tender-invite-records/index'),
        name: 'TenderRecords',
        meta: { title: '邀约记录', icon: 'el-icon-position', noCache: true }
      },
      {
        path: 'qualification',
        component: () => import('@/views/verify-manage/qualification/index'),
        name: 'Qualification',
        meta: { title: '资质审核', icon: 'el-icon-picture-outline-round', noCache: true }
      },
      {
        path: 'record',
        component: () => import('@/views/verify-manage/record/index'),
        name: 'Record',
        meta: { title: '资质审核记录', icon: 'el-icon-notebook-1', noCache: true }
      }
    ]
  },
  // 采购员标案管理
  {
    path: '/purchaser-tender',
    component: Layout,
    alwaysShow: true,
    name: 'PurchaserTender',
    meta: {
      title: '标案记录',
      icon: 'el-icon-notebook-2',
      roles: [roles.get('purchaser')]
    },
    children: [
      {
        path: 'no-approve-tender',
        component: () => import('@/views/purchaser-tender/noApprove/index'),
        name: 'NoApproveTender',
        meta: { title: '未通过标案', icon: 'el-icon-document-delete', noCache: true },
      },
      {
        path: 'adopt-tender',
        component: () => import('@/views/purchaser-tender/adopt/index'),
        name: 'AdoptTender',
        meta: { title: '已通过标案', icon: 'el-icon-document-checked', noCache: true }
      },
      {
        path: 'select-tender',
        component: () => import('@/views/purchaser-tender/selectTender/index'),
        name: 'SelectTender',
        meta: { title: '确定标案', icon: 'el-icon-thumb', noCache: true }
      },

      {
        path: '/addTender',
        component: () => import('@/views/purchaser-tender/noApprove/components/addTender'),
        name: 'AddTender',
        meta: { title: '添加标案', noCache: true },
        hidden: true,
      }

    ]
  },

  // 供应商路由
  // 资质申请
  {
    path: '/qualification-manage',
    component: Layout,
    alwaysShow: true,
    name: 'QualificationManage',
    meta: {
      title: '资质管理',
      icon: 'el-icon-notebook-2',
      roles: [roles.get('supplier')]
    },
    children: [
      {
        path: 'qualification-apply',
        component: () => import('@/views/qualification-manage/qualification-apply/index'),
        name: 'QualificationApply',
        meta: { title: '资质申请', icon: 'el-icon-takeaway-box', noCache: true }
      },
      {
        path: 'qualification-own',
        component: () => import('@/views/qualification-manage/qualification-own/index'),
        name: 'QualificationOwn',
        meta: { title: '拥有资质', icon: 'el-icon-takeaway-box', noCache: true }
      },
      {
        path: 'qualification-info',
        component: () => import('@/views/qualification-manage/qualification-info/index'),
        name: 'QualificationInfo',
        meta: { title: '申请记录', icon: 'el-icon-key', noCache: true }
      },
    ]
  },
  // {
  //   path: '/qualification-info',
  //   component: Layout,
  //   redirect: '/qualification-info/index',
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/qualification-info/index'),
  //       name: 'QualificationInfo',
  //       meta: {
  //         title: '已有资质',
  //         icon: 'el-icon-key',
  //         roles: [roles.get('supplier')] // or you can only set roles in sub nav
  //       }
  //     }
  //   ]
  // },
  // {
  //   path: '/qualification-apply',
  //   component: Layout,
  //   redirect: '/qualification-apply/index',
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/qualification-apply/index'),
  //       name: 'qualificationApply',
  //       meta: {
  //         title: '资质申请',
  //         icon: 'el-icon-takeaway-box',
  //         roles: [roles.get('supplier')] // or you can only set roles in sub nav
  //       }
  //     }
  //   ]
  // },

  // 标案申请
  {
    path: '/apply-tebder-manage',
    component: Layout,
    alwaysShow: true,
    name: 'ApplyTenderManage',
    meta: {
      title: '投标管理',
      icon: 'el-icon-notebook-2',
      roles: [roles.get('supplier')]
    },
    children: [
      {
        path: '/bid-apply',
        component: () => import('@/views/apply-tender-manage/bid-apply/index'),
        name: 'BidApply',
        meta: { title: '投标申请', icon: 'el-icon-s-order', noCache: true }
      },
      {
        path: '/invite-manage',
        component: () => import('@/views/apply-tender-manage/invite-manage/index'),
        name: 'InviteManage',
        meta: { title: '邀约管理', icon: 'el-icon-phone-outline', noCache: true }
      },
      {
        path: 'apply-tender-records',
        component: () => import('@/views/apply-tender-manage/apply-tender-records/index'),
        name: 'ApplyTenderRecords',
        meta: { title: '申请记录', icon: 'el-icon-collection-tag', noCache: true }
      },
    ]
  },
  // {
  //   path: '/bid-apply',
  //   component: Layout,
  //   redirect: '/bid-apply/index',
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/bid-apply/index'),
  //       name: 'BidApply',
  //       meta: {
  //         title: '投标申请',
  //         icon: 'el-icon-s-order',
  //         roles: [roles.get('supplier')] // or you can only set roles in sub nav
  //       }
  //     }
  //   ]
  // },
  // {
  //   path: '/invite-manage',
  //   component: Layout,
  //   redirect: '/invite-manage/index',
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/invite-manage/index'),
  //       name: 'InviteManage',
  //       meta: {
  //         title: '邀约管理',
  //         icon: 'el-icon-phone-outline',
  //         roles: [roles.get('supplier')] // or you can only set roles in sub nav
  //       }
  //     }
  //   ]
  // },
  {
    path: '/compete-tender',
    component: Layout,
    redirect: '/compete-tender/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/compete-tender/index'),
        name: 'CompeteTender',
        meta: {
          title: '竞标管理',
          icon: 'el-icon-date',
          roles: [roles.get('supplier')] // or you can only set roles in sub nav
        }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: {
          title: '个人中心', icon: 'user', noCache: true,
        }
      },
    ]
  },
  // // 公共路由
  // {
  //   path: '/personal',
  //   component: Layout,
  //   redirect: '/personal/index',
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/profile/index'),
  //       name: 'PersonalInfo',
  //       meta: {
  //         title: '个人中心',
  //         icon: 'el-icon-unlock',
  //         roles: [
  //           roles.get('supplier'),
  //           roles.get('purchaser'),
  //           roles.get('manager'),
  //           roles.get('superManager')
  //         ] // you can set roles in root nav
  //       },
  //     }
  //   ]
  // },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
